package com.zhc.community.controller;


import com.zhc.community.entity.Comment;
import com.zhc.community.entity.DiscussPost;
import com.zhc.community.entity.Event;
import com.zhc.community.event.EventProducer;
import com.zhc.community.service.CommentService;
import com.zhc.community.service.DiscussPostService;
import com.zhc.community.util.CommunityConstant;
import com.zhc.community.util.CommunityUtil;
import com.zhc.community.util.HostHolder;
import com.zhc.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant{

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private DiscussPostService discussPostService;

    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        // 触发评论事件,发送系统通知
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                .setData("postId", discussPostId);
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        eventProducer.fireEvent(event);

        if (comment.getEntityType() == ENTITY_TYPE_COMMENT){
            //触发发帖事件，将帖子信息同步到elasticsearch服务器
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)        //事件主题
                    .setUserId(comment.getUserId())        //触发事件的人的id
                    .setEntityType(ENTITY_TYPE_POST)    //事件的实体类型
                    .setEntityId(discussPostId);    //事件的实体id
            eventProducer.fireEvent(event);

            //计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey,discussPostId);

        }

        return "redirect:/discuss/detail/" + discussPostId;
    }

    //删除评论
    @PostMapping("/remove/{discussPostId}/{commentId}")
    @ResponseBody
    public String removeComment(@PathVariable("commentId") int commentId, @PathVariable("discussPostId") int discussPostId) {
        if (hostHolder.getUser() == null) {
            return CommunityUtil.getJSONString(-1, "未登录", null);
        }
        Comment comment = commentService.findCommentById(commentId);
        if (comment != null && comment.getUserId() != hostHolder.getUser().getId()) {
            return CommunityUtil.getJSONString(-1, "无法删除他人评论", null);
        }
        commentService.removeCommentById(discussPostId, commentId);
        Event event = new Event();
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            event.setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.fireEvent(event);
        }
        return CommunityUtil.getJSONString(0, "删除成功", null);
    }

}